const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "抽奖", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    showMonment: false, //是否显示须知
    showGift: false,
    showGNotWinning: false, // 未中奖

    openid: '',
    shareTitle: "",
    shareImg: '',
    flag: 0,
    prizeInfo: [], //抽奖后的奖品信息
    ruleImg: '',
    id: 0,
    activeStatus: 0, //活动状态 0:还未到预约时间 1:预约时间内 2：过了预约时间但抽奖未开始 3:抽奖中 4：既在预约又可抽奖状态 5:抽奖结束(抽奖时间已过)
    cjStatus: 0, //抽奖结果 0:未预约  1：已预约未抽奖  2:已预约已抽奖
  },
  closevideo() {
    wx.navigateBack({
      delta: 1
    });
  },
  // 未中奖显示关闭
  regretBtn() {
    wx.switchTab({
      url: '/pages/index/index'
    });
    /*this.setData({
      showGNotWinning: !this.data.showGNotWinning
    })*/
  },
  //点击我知晓了
  closeMonment() {
    let that = this;
     that.setData({
       showMonment: false
     })
  },
  showMonment() {
    let that = this;
    that.setData({
      showMonment: true
    })
  },
  //点击抽奖
  cj() {
    var that = this;
    var activeStatus = that.data.activeStatus;
    var cjStatus = that.data.cjStatus;
    //判断活动状态
    if (activeStatus == 0) {
      app.showTip('此活动还未到预约时间哦');
      return;
    } else if (activeStatus == 1) {
      app.showTip('此活动还在预约阶段哦');
      return;
    } else if ((activeStatus == 2 || activeStatus == 3 || activeStatus == 4) && cjStatus == 0) {
      app.showTip('您暂未预约本次抽奖活动哦~');
      return;
    } else if(activeStatus == 2&&cjStatus==1){
       app.showTip('抽奖暂未开始哦~');
      return;
    }else if (activeStatus == 3 || activeStatus == 4) {
      //抽奖时间内
      if (cjStatus == 0) {
        app.showTip('您暂未预约此抽奖活动哦');
        return;
      }  else if (cjStatus == 2) {
        //判断是否中奖
        if (that.data.prizeInfo['id'] > 0) {
          //中奖
          that.setData({
            showGift: true
          });
        } else {
          //不中奖
          that.setData({
            showGNotWinning: true
          });
        }
        return;
      }
    } else if (activeStatus == 5) {
      app.showTip('感谢您的参与,活动已经结束了哦');
      return;
    }

    if (that.data.flag == 0) {
      that.setData({
        flag: 1
      });

      var outTime = parseInt(that.getRandomArbitrary(100, 1500));
      //后台请求抽奖
      setTimeout(function(){
              wx.request({
        url: that.data.baseUrl + 'getLotteryPrize.php',
        method: 'POST',
        data: {
          openid: that.data.openid,
          id: that.data.id
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.status == 1) {
            that.videoContext.play();
            that.setData({
              prizeInfo: res.data.prizeInfo,
              cjStatus: 2 //修改抽奖状态为已抽奖
            });
          } else {
            app.showTip(res.data.msg);
          }
          that.setData({
            flag: 0
          });
        },
        fail: function (res) {
          app.showTip('服务器繁忙,请稍候重试');
          that.setData({
            flag: 0
          });
        }
      });
      },outTime);
    }
  },

  //生成一个随机延迟请求的毫秒数
  getRandomArbitrary(min, max) {
    return Math.random() * (max - min) + min;
  },

  videoEnd() {
    //判断是否中奖
    var is_zj = 0;
    if (this.data.prizeInfo['id'] > 0) {
      //中奖
      this.setData({
        showGift: true,
        flag: 0
      });
    } else {
      //未中奖
      this.setData({
        showGNotWinning: true,
        flag: 0
      });
    }
  },
  //查看中奖订单
  goBirthdayOrder() {
    var that = this;
    wx.navigateTo({
      url: '/pages/my/orderList?headerFlag=cj&id=' + that.data.id
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;

    //接收值
    var id = options.id;
    if (id == '' || id == undefined || id == null || id == 0) {
      app.showTip('抱歉,未获取到相关的活动id信息哦');
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        });
      }, 2000);
      return;
    } else {
      that.setData({
        id: id
      });
    }

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getLotteryVideoPageInfo(res.data.openid, id);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getLotteryVideoPageInfo(openid, id);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getLotteryVideoPageInfo(openid, id) {
    var that = this;

    wx.showLoading({
      title: '数据加载中...'
    });
    wx.request({
      url: that.data.baseUrl + 'getLotteryVideoPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid,
        'id': id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //判断是否提示预约成功
          if (res.data.isShowYy == 1) {
            setTimeout(function () {
              app.showTip(res.data.showYyContent);
            }, 1000);
          }

          //赋值页面参数
          that.setData({
            activeStatus: res.data.activeStatus,
            cjStatus: res.data.cjStatus,
            prizeInfo: res.data.cjResultInfo,
            shareTitle: res.data.activityInfo.title,
            shareImg: that.data.baseUrl + "admin/" + res.data.activityInfo.img_path
          });

          //判断抽奖后的显示弹层
          if (res.data.cjStatus == 2) {
            //已抽奖
            //判断是否中奖
            if (res.data.cjResultInfo['id'] > 0) {
              //中奖
              that.setData({
                showGift: true
              });
            } else {
              //不中奖
              that.setData({
                showGNotWinning: true
              });
            }
          }
        } else {
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function () {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.videoContext = wx.createVideoContext('video1')
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/lotteryDrawPage/detail/index?id=' + that.data.id,
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})